package com.cdu.guoxinan.www.smartcom.service.impl;

import com.cdu.guoxinan.www.smartcom.aop.Annotation.OperationLog;
import com.cdu.guoxinan.www.smartcom.domian.dto.FeeDTO;
import com.cdu.guoxinan.www.smartcom.domian.dto.QueryUserDTO;
import com.cdu.guoxinan.www.smartcom.domian.dto.ResidentQueryDTO;
import com.cdu.guoxinan.www.smartcom.domian.pojo.Fee;
import com.cdu.guoxinan.www.smartcom.domian.pojo.Resident;
import com.cdu.guoxinan.www.smartcom.domian.pojo.User;
import com.cdu.guoxinan.www.smartcom.domian.vo.FeeVO;
import com.cdu.guoxinan.www.smartcom.domian.vo.SelectOption;
import com.cdu.guoxinan.www.smartcom.mapper.FeeMapper;
import com.cdu.guoxinan.www.smartcom.service.FeeService;
import com.cdu.guoxinan.www.smartcom.service.ResidentService;
import com.cdu.guoxinan.www.smartcom.service.UserService;
import com.cdu.guoxinan.www.smartcom.util.EmailTool;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class FeeServiceImpl implements FeeService {



    @Resource
    private FeeMapper feeMapper;
    @Resource
    private ResidentService residentService;

    @Override
    public PageInfo<FeeVO> getAllFees( FeeDTO feeDTO) {

        PageHelper.startPage(feeDTO.getPage(),feeDTO.getLimit());
        List<FeeVO> allFees = feeMapper.getAllFees(feeDTO);
        PageInfo<FeeVO> pageInfo = new PageInfo<>(allFees);

        return pageInfo;
    }

    @Override
    public List<SelectOption> getFeeTypes() {
        List<SelectOption> feeTypes = feeMapper.getFeeTypes();
        List<SelectOption> selectOptions = new ArrayList<>();
        HashMap<Integer,String> map = new HashMap<>();
        map.put(1,"物业费");
        map.put(2,"车位费");
        map.put(3,"水费");
        map.put(4,"电费");
        map.put(5,"煤气费");

        if (feeTypes != null && !feeTypes.isEmpty()) {
            feeTypes.forEach(selectOption -> {

                SelectOption option = new SelectOption();
                option.setId(selectOption.getId());
                option.setValue(map.get(selectOption.getId()));
            selectOptions.add(option);
            });
            return selectOptions;
        }

        return Collections.emptyList();
    }

    @Override
    public List<SelectOption> getFeeStatus() {
        List<SelectOption> feeStatus = feeMapper.getFeeStatus();
        List<SelectOption> selectOptions = new ArrayList<>();
        HashMap<Integer,String> map = new HashMap<>();
        map.put(1,"已缴");
        map.put(2,"未缴");


        feeStatus.forEach(selectOption -> {
            SelectOption option = new SelectOption();
            option.setId(selectOption.getId());
            option.setValue(map.get(selectOption.getId()));
            selectOptions.add(option);
        });
        return selectOptions;

    }

    @Override
    public int addResidentFee(FeeVO feeVO) {
        // 根据居民名称查询是否有该居民存在TODO
        ResidentQueryDTO residentQueryDTO = new ResidentQueryDTO();
        residentQueryDTO.setName(feeVO.getName());
        PageInfo<Resident> pageInfo = residentService.queryPage(residentQueryDTO);
        long i = pageInfo.getTotal();

       if (i>0){
           Long id = pageInfo.getList().get(0).getResidentId();
           feeVO.setResidentId(id);
           System.out.println("feeVO:"+feeVO.getResidentId());

           String email = pageInfo.getList().get(0).getEmail(); // 获取用户邮箱
           if ( email != null && !email.isEmpty() && EmailTool.isValidEmail(email)) {
               Integer feeType = feeVO.getFeeType(); // 费用类型
               Integer status = feeVO.getStatus();   // 缴费状态

               String subject = "物业费用通知";
               String content = "";

// 根据状态生成邮件内容
               if (status != null && status == 1) {
                   String feeTypeName = feeType == 1 ? "物业费" : "车位费";
                   content = String.format("尊敬的业主，您的%s已成功缴纳，请查收。", feeTypeName);
               } else {
                   String feeTypeName = feeType == 1 ? "物业费" : "车位费";
                   content = String.format("提醒：您有一笔未缴纳的%s，请尽快完成缴费，以免影响正常使用。", feeTypeName);
               }



// 发送邮件
               try {
                   boolean isSent = EmailTool.sendSimpleEmail(email, subject, content);
                   if (!isSent) {
                       // 可选：记录未发送成功的原因
                       System.err.println("邮件发送失败，收件人: " + email);
                   }
               } catch (Exception e) {
                   // 捕获所有异常，防止主流程被中断
                   System.err.println("邮件发送过程中发生异常，收件人: " + email);
                   e.printStackTrace(); // 可替换为 log.error(...) 以便使用日志框架
               }
           }
          return feeMapper.addResidentFee(feeVO);
       }
        return -1; // 没有该居民

    }

    @Override
    public int deleteResidentFee(Integer feeId) {

        return  feeMapper.deleteResidentFee(feeId);
    }

    @Override
    public int updateResidentFee(Fee fee) {

        return feeMapper.updateResidentFee(fee);
    }

    @Override
    public int deleteResidentFees(Integer[] feeIds) {
        return feeMapper.deleteResidentFees(feeIds);
    }

    @Override
    public List<FeeVO> getById(Integer residentId) {

        return feeMapper.getById(residentId);
    }
}
